package com.yourcompany.web.controller;

import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yourcompany.api.dto.ChargingOrderCreateDTO;
import com.yourcompany.api.dto.OrderQueryDTO;
import com.yourcompany.api.entity.ChargingOrder;
import com.yourcompany.api.service.ChargingOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "充电订单管理")
@RestController
@RequestMapping("/api/order")
public class ChargingOrderController {

    @Autowired
    private ChargingOrderService orderService;

    @Operation(summary = "创建订单")
    @PostMapping
    public SaResult createOrder(@Validated @RequestBody ChargingOrderCreateDTO order) {
        return SaResult.ok().setData(orderService.createChargingOrder(order));
    }

    @Operation(summary = "开始充电")
    @PostMapping("/{orderNo}/start")
    public SaResult startCharging(@PathVariable String orderNo) {
        orderService.startCharging(orderNo);
        return SaResult.ok();
    }

    @Operation(summary = "结束充电")
    @PostMapping("/{orderNo}/end")
    public SaResult endCharging(@PathVariable String orderNo) {
        orderService.endCharging(orderNo);
        return SaResult.ok();
    }

    @Operation(summary = "取消订单")
    @PostMapping("/{orderNo}/cancel")
    public SaResult cancelOrder(@PathVariable String orderNo) {
        orderService.cancelOrder(orderNo);
        return SaResult.ok();
    }

    @Operation(summary = "支付订单")
    @PostMapping("/{orderNo}/pay")
    public SaResult payOrder(
            @PathVariable String orderNo,
            @RequestParam String paymentMethod) {
        return SaResult.ok().setData(orderService.payOrder(orderNo, paymentMethod));
    }

    @Operation(summary = "查询支付结果")
    @GetMapping("/{orderNo}/payment")
    public SaResult queryPayment(@PathVariable String orderNo) {
        return SaResult.ok().setData(orderService.queryPayment(orderNo));
    }

    @Operation(summary = "分页查询订单")
    @GetMapping("/page")
    public SaResult pageOrders(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            OrderQueryDTO queryDTO) {
        Page<ChargingOrder> page = new Page<>(pageNum, pageSize);
        return SaResult.ok().setData(orderService.pageOrders(page, queryDTO));
    }
} 